//
//  OASetMeetingOpinionController.m
//  OASystem
//
//  Created by zhoupengfei on 15/11/22.
//  Copyright © 2015年 zpf. All rights reserved.
//
#import "GDataDefines.h"

#import "OASetMeetingOpinionController.h"
#import "OAInputOpinionView.h"
#import "OAInputRootView.h"
#import "HttpTools.h"
#import "Toast+UIView.h"
@interface OASetMeetingOpinionController ()
@property (weak, nonatomic) IBOutlet OAInputRootView *inputRootView;
@property (weak, nonatomic) IBOutlet UITextView *textView;


- (IBAction)addAction:(UIButton *)sender;
@property(nonatomic,copy)NSString * body;

@end

@implementation OASetMeetingOpinionController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self setRightItem];
}
- (void)viewWillAppear:(BOOL)animated{

   [self requestData];
}
-(void)requestData{
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"unid":self.identifier},@{@"userId":userId}];
    
    NSString * url = nil;
    NSString * method = nil;
  url = @"exchexchange";
  method = @"getMeetSuggestion";
    [HttpTools requestWithURL:url arrays:array method:method success:^(id JSON) {
        NSArray * dataArrary = JSON[@"data"];
        if (dataArrary.count) {
        for(int i = 1;i<dataArrary.count;i++){
              [self.inputRootView addInputOpinionView];

            }

            [self.inputRootView updata:dataArrary];
                [self.tableView reloadData];

                    }
       
    } failure:^(NSError *error) {
 
    }];
    
}


-(void)setRightItem{

    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(submitAction)];
    
    self.navigationItem.rightBarButtonItem = rightItem;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 84;
    }else if (indexPath.row == 1) {
        return [self.inputRootView inputRootViewHeight];
    }else if (indexPath.row == 2){
        return 135;
    }
    return 44;
}


- (IBAction)addAction:(UIButton *)sender {
    [self.inputRootView addInputOpinionView];
    [self.tableView reloadData];
}
-(void)submitAction{
    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];

    [self.view endEditing:YES];
    
    NSArray * inputInfoArray = [self.inputRootView getInfo];
    NSMutableArray *name = [NSMutableArray array];
     NSMutableArray *job = [NSMutableArray array];
      NSMutableArray *phone = [NSMutableArray array];
      for(int i = 0;i<inputInfoArray.count;i++){
      
NSDictionary *dic = inputInfoArray[i];
 if([[dic allKeys]  containsObject: @"name"]){
[name addObject:dic[@"name"]];
}
 if([[dic allKeys]  containsObject: @"jobTitle"]){
[job addObject:dic[@"jobTitle"]];
}
 if([[dic allKeys]  containsObject: @"connectMetod"]){
[phone addObject:dic[@"connectMetod"]];
}

            }
NSString *namestring = [name componentsJoinedByString:@","];
    NSString *jobstring = [job componentsJoinedByString:@","];
    NSString *phonestring = [phone componentsJoinedByString:@","];
    
    
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:inputInfoArray forKey:@"info"];
    if (self.textView.text.length !=0) {
        [dict setObject:self.textView.text forKey:@"reason"];
    }else{
        [dict setObject:@"" forKey:@"reason"];
    }
    
    NSData * data  = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL];
    NSString * infoString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
      NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
   

  
 self.body = [NSString stringWithFormat:@"&lt;?xml version=\'1.0\' encoding=\'utf-8\'?&gt;\n"
                     "&lt;交换 xmlns=\'exch.jhsx.gov.cn/data\' xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\'&gt;\n"
                     "&lt;数据&gt;\n"
                     "&lt;数据要素&gt;\n"
                     "&lt;姓名&gt;\n"
                            "%@"
                     "&lt;/姓名&gt;\n"
                     "&lt;职务&gt;\n"
                           "%@"
                     "&lt;/职务&gt;\n"
                     "&lt;联系方式&gt;\n"
                         "%@"
                     "&lt;/联系方式&gt;\n"
                      "&lt;不参加会议原因&gt;\n"
                           "%@"
                     "&lt;/不参加会议原因&gt;\n"
                     "&lt;/数据要素&gt;\n"
                     "&lt;/数据&gt;\n"
                     "&lt;/交换&gt;",
                   namestring,
                     jobstring,
                    phonestring,
                    self.textView.text
                     ];
        
//     NSArray * array =@[@{@"userId":userId},@{@"unid":self.identifier},@{@"formxml":_body}];
    
          NSArray * array =@[@{@"unid":self.identifier},@{@"userId":userId},@{@"formxml":_body}];

    
      [HttpTools postWithURL:KexchexchangeURL arrays:array method:KSetSubmitMeeting success:^(id JSON) {
              [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
 GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData: [JSON dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
        GDataXMLElement *rootElement = [doc rootElement];
        NSArray *arrData= [rootElement elementsForName:@"info"];
        NSString * message= nil;
        for (GDataXMLElement * subXMLElement in arrData) {
            GDataXMLElement * element = [[subXMLElement elementsForName:@"msg"] objectAtIndex:0];
            message  = [element stringValue];
        }
        NSLog(@"%@",message);
if ([message isEqualToString:@"33"]) {
            [self.view.window makeToast:@"提交意见成功"];
                    [self.navigationController popViewControllerAnimated:YES];
}else{

[self.view.window makeToast:@"提交意见失败"];
}
    } failure:^(NSError *error) {
        [self.view.window makeToast:@"提交意见失败"];
    }];

}

//-(void)submitAction{
//    [self.view endEditing:YES];
//    
//    NSArray * inputInfoArray = [self.inputRootView getInfo];
//    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
//    [dict setObject:inputInfoArray forKey:@"info"];
//    if (self.textView.text.length !=0) {
//        [dict setObject:self.textView.text forKey:@"reason"];
//    }else{
//        [dict setObject:@"" forKey:@"reason"];
//    }
//    
//    NSData * data  = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL];
//    NSString * infoString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//    
//    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
//    NSArray * array =@[@{@"userId":userId},@{@"unid":self.identifier},@{@"formxml":infoString}];
//    //KexchexchangeURL
//    [HttpTools requestWithURL:KexchexchangeURL arrays:array method:KSetSubmitMeeting success:^(id JSON) {
//        [self.view.window makeToast:@"提交意见成功"];
//        [self.navigationController popViewControllerAnimated:YES];
//    } failure:^(NSError *error) {
//        [self.view.window makeToast:@"提交意见失败"];
//    }];
//
//}
@end
